package com.cuput.partybuilding.publiclibrary.manager;

import android.content.Context;

import com.devin.core.network.OKHttpClientFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.OkHttpClient;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscription;

/**
 * Created by apple on 2016/10/23.
 */
public final class RetrofitServiceManager {

    private static Retrofit retrofit ;

    public static Map<String, ArrayList<Subscription>> subscriptionMap ;

    public static void init(Context context, String baseUrl, OkHttpClient okHttpClient, Converter.Factory... converterFactorys) {
        retrofit = getRetrofit(context,baseUrl,okHttpClient,converterFactorys);
        subscriptionMap = new HashMap<>();
    }

    /**
     * 初始化Retrofit，默认使用GsonConverterFactory和RxJavaCallAdapterFactory
     * @param context
     * @param baseUrl
     * @param converterFactorys
     * @return
     */
    private static Retrofit getRetrofit(Context context, String baseUrl, OkHttpClient okHttpClient, Converter.Factory... converterFactorys){

        if (converterFactorys == null || converterFactorys.length == 0) {
            converterFactorys = new Converter.Factory[1];
            converterFactorys[0] = GsonConverterFactory.create();
        }

        if(okHttpClient == null) {
            okHttpClient = OKHttpClientFactory.defaultOkHttpClient(context);
        }

        Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create());

        for(Converter.Factory converterFactory : converterFactorys) {
            retrofitBuilder.addConverterFactory(converterFactory);
        }

        return retrofitBuilder.build();
    }

    public static void tagToSubscription(String tag, Subscription subscription) {
        ArrayList<Subscription> subscriptions = subscriptionMap.get(tag);
        if (subscription == null) {
            subscriptions = new ArrayList<>();
        }
        subscriptions.add(subscription);
        subscriptionMap.put(tag,subscriptions);
    }

    public static void clearInvalidTag(){
        for (String tag : subscriptionMap.keySet()) {
            ArrayList<Subscription> subscriptions = subscriptionMap.get(tag);
            if (subscriptions == null || subscriptions.size() == 0) {
                subscriptionMap.remove(tag);
            } else {
                for (int i = 0 ; i < subscriptions.size() ; i++) {
                    Subscription subscription = subscriptions.get(i);
                    if (subscription.isUnsubscribed()) {
                        subscriptions.remove(subscription);
                    }
                }
            }
        }
    }

    public static void cancelTag(String tag) {
        ArrayList<Subscription> subscriptions = subscriptionMap.get(tag);
        if (subscriptions != null && subscriptions.size() > 0) {
            for (Subscription subscription : subscriptions) {
                if (!subscription.isUnsubscribed()){
                    subscription.unsubscribe();
                }
            }
            subscriptionMap.remove(tag);
        }
    }

    public static ArrayList<Subscription> getSubscriptionsWithTag(String tag) {
        return subscriptionMap.get(tag) == null ? new ArrayList<Subscription>() : subscriptionMap.get(tag);
    }

    public static void cancelAllTag() {
        for (String tag : subscriptionMap.keySet()) {
            cancelTag(tag);
        }
    }


}
